package com.ly.gen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.gen.entity.GenDataTable;
import com.ly.gen.service.IGenDataTableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统数据库表
 *
 * @author luoyun
 * @Date: 2025-06-16
 */
@Tag(name = "系统数据库表")
@Slf4j
@RestController
@RequestMapping("/online/genDataTable")
@RequiredArgsConstructor
public class GenDataTableController {

    private final IGenDataTableService genDataTableService;

    /**
     * 查询-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询-id")
    @GetMapping("/getId")
    public R<GenDataTable> getId(@RequestParam(name = "id") String id) {
        return genDataTableService.getId(id);
    }

    /**
     * 列表
     *
     * @param entity
     * @return
     */
    @Operation(summary = "列表")
    @GetMapping("/getList")
    public R<List<GenDataTable>> getList(GenDataTable entity) {
        return genDataTableService.getList(entity);
    }

    /**
     * 分页
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("/getPage")
    public R<IPage<GenDataTable>> getPage(
            GenDataTable entity,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return genDataTableService.getPage(entity, new Page<GenDataTable>(pageNo, pageSize));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    public R<String> add(@RequestBody GenDataTable entity) {
        return genDataTableService.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping("/edit")
    public R<String> edit(@RequestBody GenDataTable entity) {
        return genDataTableService.edit(entity);
    }

    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除-id")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return genDataTableService.delete(id);
    }

    /**
     * 批量新增或修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "批量新增或修改")
    @PostMapping("/updateBatch")
    public R updateBatch(@Validated @RequestBody CommonBatchUpdateDTO<GenDataTable> dto) {
        return genDataTableService.updateBatch(dto);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        return genDataTableService.deleteBatch(ids);
    }

    /**
     * 初始化
     *
     * @return
     */
    @Operation(summary = "初始化")
    @PostMapping("/init")
    public R init() {
        return genDataTableService.init();
    }

    /**
     * 同步表字段
     *
     * @return
     */
    @Operation(summary = "同步表字段")
    @PostMapping("/updateGenColumn/{id}")
    public R<?> updateGenColumn(@PathVariable("id") String id) {
        return genDataTableService.updateGenColumn(id);
    }

    /**
     * 同步表主键为id
     *
     * @return
     */
    @Operation(summary = "同步表主键为id")
    @PostMapping("/updateTableId/{id}")
    public R<?> updateTableId(@PathVariable("id") String id) {
        return genDataTableService.updateTableId(id);
    }
}
